#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import webapp
import cgi
import feedparser

html5header = """<!DOCTYPE HTML>
<html lang=en>
<head>
<meta charset=utf-8>
<title>%s</title>
<link type="text/css" rel="stylesheet" href="/css/river.css" />
</head>
<body>"""

html5footer = '</body></html>'

feedlist=('http://feeds.reuters.com/reuters/topNews','http://www.nytimes.com/services/xml/rss/userland/HomePage.xml','http://hosted.ap.org/lineups/USHEADS-rss_2.0.xml?SITE=WSAW&SECTION=HOME')
class MainHandler(webapp.RequestHandler):

  def get(self):
    self.response.out.write(html5header % 'News River in XOXO + hAtom')
    self.response.out.write('<ul class="XOXO">')
    for url in feedlist:
    
        d = feedparser.parse(url)
        self.response.out.write('<li><a class="feed-title" href="%s">%s</a> <a href="%s" class="feed-url"><img src="/plain/feed-icon-14x14.png"></a>\n<ul class="hfeed">\n' % (cgi.escape(d.feed.link),cgi.escape(d.feed.title),cgi.escape(url)))
    
        for entry in d.entries:
            self.response.out.write('<li class="hentry"><a href="%s" rel="bookmark" class="entry-title">%s</a> <span class="entry-summary">%s</span></li>\n' % (cgi.escape(entry.link),cgi.escape(entry.title),entry.summary))
        self.response.out.write('</ul>\n</li>\n')
    self.response.out.write('</ul>\n')
    self.response.out.write(html5footer)

def main():
  application = webapp.WSGIApplication([('/', MainHandler)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
